package ex01.pyrmont;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import ex02.pyrmont.RequestDispatcher;

public class Request {

	private InputStream inputStream;
	private String uri;
	
	public Request(InputStream in) {
		inputStream = in;
	}
	
	public void parse() {
		
		StringBuffer request = new StringBuffer(2048);
		int i;
		byte[] buffer = new byte[2048];
		try {
			i = inputStream.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			i = -1;
		}
		for (int j = 0; j < i; j++) {
			request.append( (char) buffer[j] );
		}
		System.out.println("request: " + request.toString());
		uri = parseUri(request.toString());
	}
	
	private String parseUri(String requestString) {
		
		int index1, index2;
		index1 = requestString.indexOf(" ");
		if (index1 != -1) {
			index2 = requestString.indexOf(" ", index1 + 1);
			if (index2 > index1) {
				return requestString.substring(index1 + 1, index2);
			}
		}
		return null;
	}
	
	public String getUri() {
		
		return uri;
	}
	
	// TODO 下面的方法还没有实现
	public Object getAttribute(String attributeName) {
		
		return null;
	}
	
	public Enumeration<String> getAttributeNames() {
		
		return null;
	}
	
	public String getRealPath(String path) {
		
		return null;
	}
	
	public RequestDispatcher getRequestDispatcher(String path) {
		
		return null;
	}
	
	public boolean isSecure() {
	
		return false;
	}
	
	public String getCharacterEncoding() {
		
		return null;
	}
	
	public int getContentLength() {
		
		return 0;
	}
	
	public void removeAttribute(String attributeName) {
		
	}
	
	public void setAttribute(String key, Object value) {
		
	}
	
	public void setCharacterEncoding(String encoding) {
		
	}
}
